package com.open.jack.jk_mpchart;

import android.graphics.Canvas;
import android.graphics.Path;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.renderer.YAxisRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.ViewPortHandler;

/**
 * @TODO
 * @author: clf
 * @since: 2020/11/26
 */
public class MyYAxisRenderer extends YAxisRenderer {
    public boolean mDrawGridInnerLines = false;
    public MyYAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer trans) {
        super(viewPortHandler, yAxis, trans);
    }

    @Override
    protected Path linePath(Path p, int i, float[] positions) {
        if (mDrawGridInnerLines){
            p.moveTo(mViewPortHandler.offsetLeft()-5, positions[i + 1]);
            p.lineTo(mViewPortHandler.contentRight(), positions[i + 1]);
        }else {
            float x = mViewPortHandler.offsetLeft();
            p.moveTo(x, positions[i + 1]);
            p.lineTo(x-5, positions[i + 1]);
        }
        return p;
    }

    @Override
    public void renderGridLines(Canvas c) {
        if (!mYAxis.isEnabled())
            return;

        if (mYAxis.isDrawGridLinesEnabled()) {

            int clipRestoreCount = c.save();
            //c.clipRect(getGridClippingRect());

            float[] positions = getTransformedPositions();

            mGridPaint.setColor(mYAxis.getGridColor());
            mGridPaint.setStrokeWidth(mYAxis.getGridLineWidth());
            mGridPaint.setPathEffect(mYAxis.getGridDashPathEffect());

            Path gridLinePath = mRenderGridLinesPath;
            gridLinePath.reset();

            // draw the grid
            for (int i = 0; i < positions.length; i += 2) {

                // draw a path because lines don't support dashing on lower android versions
                c.drawPath(linePath(gridLinePath, i, positions), mGridPaint);
                gridLinePath.reset();
            }

            c.restoreToCount(clipRestoreCount);
        }

        if (mYAxis.isDrawZeroLineEnabled()) {
            drawZeroLine(c);
        }
    }
}
